<!DOCTYPE html>
<html lang="zh-CN">
    <head>
    <meta charset="utf-8">

    

    <!-- 渲染优化 -->
    <meta name="renderer" content="webkit">
    <meta name="force-rendering" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="HandheldFriendly" content="True" >
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <!--icon-->

    
        <link rel="shortcut icon" href="https://s1.imgpp.com/2022/01/28/owl.png">
    
    
    
    
    


    <!-- meta -->


<title>laravel学习笔记 | 猫头鹰的BLOG</title>


    <meta name="keywords" content="路由, 控制器, 猫头鹰的博客">




    <!-- OpenGraph -->
 
    <meta name="description" content="环境搭建步骤   下载composer网址 检查composer安装完成命令：php artisan –version 创建一个laravel项目：composer create-project laravel&#x2F;laravel 启动laravel：php artisan serve 服务器配置：选择的时nginx 在Nginx1.15.11\conf\vhost下的域名.confs文件下添加：tr">
<meta property="og:type" content="article">
<meta property="og:title" content="laravel学习笔记">
<meta property="og:url" content="http://example.com/2022/01/26/laravel/index.html">
<meta property="og:site_name" content="猫头鹰的BLOG">
<meta property="og:description" content="环境搭建步骤   下载composer网址 检查composer安装完成命令：php artisan –version 创建一个laravel项目：composer create-project laravel&#x2F;laravel 启动laravel：php artisan serve 服务器配置：选择的时nginx 在Nginx1.15.11\conf\vhost下的域名.confs文件下添加：tr">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://s1.imgpp.com/2022/01/28/pexels-alesia-kozik-6770609.jpg">
<meta property="article:published_time" content="2022-01-25T16:00:00.000Z">
<meta property="article:modified_time" content="2022-04-04T03:53:43.600Z">
<meta property="article:author" content="张程">
<meta property="article:tag" content="路由">
<meta property="article:tag" content="控制器">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://s1.imgpp.com/2022/01/28/pexels-alesia-kozik-6770609.jpg">


    
<link rel="stylesheet" href="/css/style/main.css">
 

    
    
    
        <link rel="stylesheet" id="hl-default-theme" href="https://cdn.jsdelivr.net/npm/highlight.js@10.1.2/styles/arduino-light.min.css" media="none" >
        
            <link rel="stylesheet" id="hl-dark-theme" href="https://cdn.jsdelivr.net/npm/highlight.js@10.1.2/styles/obsidian.min.css" media="none">
        
    

    

    
    
<link rel="stylesheet" href="/css/style/dark.css">

    
<script src="/js/darkmode.js"></script>



     
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
 

    <!-- custom head -->

<meta name="generator" content="Hexo 6.0.0"></head>

    <body>
        <div id="app">
            <header class="header">
    <div class="header__left">
        <a href="/" class="button">
            <span class="logo__text">猫头鹰</span>
        </a>
    </div>
    <div class="header__right">
        
            <div class="navbar__menus">
                
                    <a href="/" class="navbar-menu button">首页</a>
                
                    <a href="/tags/" class="navbar-menu button">标签</a>
                
                    <a href="/categories/" class="navbar-menu button">分类</a>
                
                    <a href="/archives/" class="navbar-menu button">归档</a>
                
            </div>
        
        
        
    <a href="/search/" id="btn-search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" width="24" height="24" fill="currentColor" stroke="currentColor" stroke-width="32"><path d="M192 448c0-141.152 114.848-256 256-256s256 114.848 256 256-114.848 256-256 256-256-114.848-256-256z m710.624 409.376l-206.88-206.88A318.784 318.784 0 0 0 768 448c0-176.736-143.264-320-320-320S128 271.264 128 448s143.264 320 320 320a318.784 318.784 0 0 0 202.496-72.256l206.88 206.88 45.248-45.248z"></path></svg>
    </a>


        
        
    <a href="javaScript:void(0);" id="btn-toggle-dark">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
    </a>


        
            <a class="dropdown-icon button" id="btn-dropdown" tabindex="0"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" width='24' height='24' fill="none" stroke="currentColor" stroke-width="0.7" stroke-linecap="round" stroke-linejoin="round"><path fill="currentColor" d="M3.314,4.8h13.372c0.41,0,0.743-0.333,0.743-0.743c0-0.41-0.333-0.743-0.743-0.743H3.314c-0.41,0-0.743,0.333-0.743,0.743C2.571,4.467,2.904,4.8,3.314,4.8z M16.686,15.2H3.314c-0.41,0-0.743,0.333-0.743,0.743s0.333,0.743,0.743,0.743h13.372c0.41,0,0.743-0.333,0.743-0.743S17.096,15.2,16.686,15.2z M16.686,9.257H3.314c-0.41,0-0.743,0.333-0.743,0.743s0.333,0.743,0.743,0.743h13.372c0.41,0,0.743-0.333,0.743-0.743S17.096,9.257,16.686,9.257z"></path></svg></a>
            <div class="dropdown-menus" id="dropdown-menus">
                
                    <a href="/" class="dropdown-menu button">首页</a>
                
                    <a href="/tags/" class="dropdown-menu button">标签</a>
                
                    <a href="/categories/" class="dropdown-menu button">分类</a>
                
                    <a href="/archives/" class="dropdown-menu button">归档</a>
                
            </div>
        
    </div>
</header>


            <main class="main">
    

<div class="post-title">
    <h1 class="post-title__text">
        laravel学习笔记
    </h1>
    <div class="post-title__meta">
        <a href="/archives/2022/01/" class="post-meta__date button">2022-01-26</a>
        
    <span class="separate-dot"></span><a href="/categories/laravel/" class="button">laravel</a>

 
        
    
     
    <span id="busuanzi_container_page_pv" hidden>
        <span class="separate-dot"></span>
        <span></span>
        <span id="busuanzi_value_page_pv"></span>
        <span>次观看</span>
    </span>



 

 
    </div>
</div>



<article class="post content-card">
    <div class="post__header">
    
        <div class="post-thumbnail lazy" data-bg="https://s1.imgpp.com/2022/01/28/pexels-alesia-kozik-6770609.jpg"></div>
    
</div>
    <div class="post__content">
        <h2 id="环境搭建"><a href="#环境搭建" class="headerlink" title="环境搭建"></a>环境搭建</h2><h4 id="步骤"><a href="#步骤" class="headerlink" title="步骤"></a>步骤</h4><!-- fancybox配置项 -->
<!-- ![报纸](https://i.loli.net/2020/11/27/CUZ6KXcRBTdjIGH.jpg) -->
<ol>
<li>下载composer<a target="_blank" rel="noopener" href="https://getcomposer.org/download/">网址</a></li>
<li>检查composer安装完成命令：php artisan –version</li>
<li>创建一个laravel项目：composer create-project laravel/laravel</li>
<li>启动laravel：php artisan serve</li>
<li>服务器配置：选择的时nginx</li>
<li>在Nginx1.15.11\conf\vhost下的域名.confs文件下添加：try_files $uri $uri/ /index.php?$query_string*;*</li>
<li>在c盘system32中的hosts文件下添加：127.0.0.1 域名</li>
</ol>
<h2 id="路由"><a href="#路由" class="headerlink" title="路由"></a>路由</h2><ol>
<li>基本路由</li>
</ol>
<pre><code class="hljs php">Route::get(<span class="hljs-string">&#x27;/hello1&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Hello word1!&quot;</span>;
&#125;);</code></pre>

<ol start="2">
<li>多种请求方式的路由</li>
</ol>
<pre><code class="hljs php">Route::any(<span class="hljs-string">&quot;/any&quot;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>&#123;
    dd(<span class="hljs-string">&quot;any&quot;</span>);
&#125;);
<span class="hljs-comment">//或者</span>
Route::match([<span class="hljs-string">&quot;get&quot;</span>,<span class="hljs-string">&quot;post&quot;</span>],<span class="hljs-string">&quot;/login&quot;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>&#123;
    dd(<span class="hljs-string">&quot;match&quot;</span>);
&#125;);</code></pre>

<ol start="3">
<li>定向路由</li>
</ol>
<pre><code class="hljs php">Route::redirect(<span class="hljs-string">&quot;/redirect1&quot;</span>,<span class="hljs-string">&quot;/redirect2&quot;</span>,<span class="hljs-number">301</span>);</code></pre>

<ol start="4">
<li>视图路由</li>
</ol>
<pre><code class="hljs php">Route::view(<span class="hljs-string">&quot;/wel&quot;</span>,<span class="hljs-string">&quot;welcome&quot;</span>);</code></pre>

<ol start="5">
<li>必填参数路由</li>
</ol>
<pre><code class="hljs php">Route::get(<span class="hljs-string">&quot;/params/&#123;id&#125;&quot;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"><span class="hljs-variable">$id</span></span>)</span>&#123;
    <span class="hljs-keyword">return</span> <span class="hljs-variable">$id</span>;
&#125;);</code></pre>

<ol start="6">
<li>可选参数路由</li>
</ol>
<pre><code class="hljs php">Route::get(<span class="hljs-string">&quot;/optional/&#123;id?&#125;&quot;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"><span class="hljs-variable">$id</span>=<span class="hljs-string">&quot;default&quot;</span></span>)</span>&#123;
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;可选参数：&quot;</span>.<span class="hljs-variable">$id</span>;
&#125;);</code></pre>

<ol start="7">
<li>正则约束=&gt;路由参数</li>
</ol>
<pre><code class="hljs php">Route::get(<span class="hljs-string">&quot;/regular/&#123;id&#125;&quot;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"><span class="hljs-variable">$id</span></span>)</span>&#123;
    <span class="hljs-keyword">return</span> <span class="hljs-variable">$id</span>;
&#125;)-&gt;where(<span class="hljs-string">&#x27;id&#x27;</span>,<span class="hljs-string">&quot;[0-9]+&quot;</span>);

<span class="hljs-comment">//或者</span>
Route::get(<span class="hljs-string">&quot;/regular/&#123;name&#125;&quot;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"><span class="hljs-variable">$name</span></span>)</span>&#123;
    <span class="hljs-keyword">return</span> <span class="hljs-variable">$name</span>;
&#125;)-&gt;where(<span class="hljs-string">&#x27;id&#x27;</span>,<span class="hljs-string">&quot;[a-zA-Z]+&quot;</span>);

<span class="hljs-comment">//全局约束  在providers/RouteServiceProvider.php的boot类中</span>
Route::pattern(<span class="hljs-string">&quot;id&quot;</span>,<span class="hljs-string">&quot;[0-9]+&quot;</span>);</code></pre>

<ol start="8">
<li>命名</li>
</ol>
<pre><code class="hljs php">Route::get(<span class="hljs-string">&#x27;givename&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;
    <span class="hljs-comment">// 通过路由名称生成 URL</span>
    <span class="hljs-keyword">return</span> route(<span class="hljs-string">&#x27;givename&#x27;</span>);
&#125;)-&gt;name(<span class="hljs-string">&#x27;givename&#x27;</span>);

<span class="hljs-comment">//含参数</span>
Route::get(<span class="hljs-string">&quot;/user/&#123;id&#125;/&#123;name&#125;/givename_params&quot;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>&#123;
    <span class="hljs-keyword">return</span> route(<span class="hljs-string">&#x27;givename_params&#x27;</span>,[<span class="hljs-string">&#x27;id&#x27;</span>=&gt;<span class="hljs-number">123</span>,<span class="hljs-string">&quot;name&quot;</span>=&gt;<span class="hljs-string">&#x27;lisi&#x27;</span>]);
&#125;)-&gt;name(<span class="hljs-string">&quot;givename_params&quot;</span>);</code></pre>

<h2 id="控制器"><a href="#控制器" class="headerlink" title="控制器"></a>控制器</h2><ol>
<li>基本控制器</li>
</ol>
<p> <em>创建命令</em></p>
<pre><code class="hljs php">php artisan make:controller controllername</code></pre>

<p><em>路由格式</em></p>
<pre><code class="hljs ph">Route::get(&quot;url&quot;,[controllername::class,&quot;functionname&quot;])</code></pre>

<ol start="2">
<li>单行为控制器</li>
</ol>
<p> <em>创建命令</em></p>
<pre><code class="hljs php">php artisan make:controller controllername --invakable</code></pre>

<p><em>路由格式</em></p>
<pre><code class="hljs php">Route::get(<span class="hljs-string">&quot;url&quot;</span>,controllername::class)</code></pre>

<ol start="3">
<li>资源控制器</li>
</ol>
<p> <em>创建命令</em></p>
<pre><code class="hljs php">php artisan make:controller controllername --resource</code></pre>

<p><em>路由格式</em></p>
<pre><code class="hljs php">Route::get(<span class="hljs-string">&quot;name&quot;</span>,controllername::class)-&gt;names([<span class="hljs-string">&#x27;index&#x27;</span>=&gt;<span class="hljs-string">&quot;index2&quot;</span>])-&gt;parameters([<span class="hljs-string">&#x27;name&#x27;</span>=&gt;<span class="hljs-string">&#x27;name2&#x27;</span>])</code></pre>
<h2 id="接收参数"><a href="#接收参数" class="headerlink" title="接收参数"></a>接收参数</h2><h4 id="接收路由参数两种方法"><a href="#接收路由参数两种方法" class="headerlink" title="接收路由参数两种方法"></a>接收路由参数两种方法</h4><ol>
<li>常规方法<blockquote>
<p>Route::get(“/demo/{id}”,[DemoController::class,”demo”]);</p>
</blockquote>
<pre><code class="hljs php"><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">demo</span>(<span class="hljs-params"><span class="hljs-variable">$id</span></span>)</span>&#123;
	dump(<span class="hljs-variable">$id</span>);
&#125;</code></pre></li>
<li>通过Request请求类的<strong>动态属性</strong>,接收路由参数<blockquote>
<p>Route::get(“/demo/{id}”,[DemoController::class,”demo”]);</p>
</blockquote>
<pre><code class="hljs php"><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">demo</span>(<span class="hljs-params">Request <span class="hljs-variable">$request</span></span>)</span>&#123;
	dump(<span class="hljs-variable">$request</span>-&gt;id);
&#125;</code></pre></li>
</ol>
<h4 id="Request请求类的其他方法"><a href="#Request请求类的其他方法" class="headerlink" title="Request请求类的其他方法"></a>Request请求类的其他方法</h4><ol>
<li><p>url()</p>
<blockquote>
<p>获取请求的url，不包含url参数</p>
</blockquote>
<pre><code class="hljs php">dump(<span class="hljs-variable">$request</span>-&gt;url());</code></pre></li>
<li><p>method()</p>
<blockquote>
<p>获取提交方式</p>
</blockquote>
<pre><code class="hljs php">dump(<span class="hljs-variable">$request</span>-&gt;method());</code></pre></li>
<li><p>has(“”)</p>
<blockquote>
<p>判断是否存在参数</p>
</blockquote>
<pre><code class="hljs php">dump(<span class="hljs-variable">$request</span>-&gt;has(<span class="hljs-string">&quot;name&quot;</span>));</code></pre></li>
<li><p>only(“”)</p>
<blockquote>
<p>只获取此参数的值</p>
</blockquote>
<pre><code class="hljs php">dump(<span class="hljs-variable">$request</span>-&gt;only(<span class="hljs-string">&quot;name&quot;</span>));</code></pre></li>
<li><p>except(“”)</p>
<blockquote>
<p>获取除该参数以外参数的值</p>
</blockquote>
<pre><code class="hljs php">dump(<span class="hljs-variable">$request</span>-&gt;except(<span class="hljs-string">&quot;name&quot;</span>));</code></pre></li>
<li><p>old(“”)</p>
<blockquote>
<p>将上次请求的数据保存到old中<br>在视图中显示,使用全局old(), </p>
</blockquote>
<pre><code class="hljs php">dump(<span class="hljs-variable">$request</span>-&gt;old(<span class="hljs-string">&quot;name&quot;</span>));</code></pre>
<pre><code class="hljs html">&#123;&#123;old(&quot;&quot;)&#125;&#125;</code></pre></li>
</ol>
<h2 id="视图"><a href="#视图" class="headerlink" title="视图"></a>视图</h2><h4 id="向视图传递数据"><a href="#向视图传递数据" class="headerlink" title="向视图传递数据"></a>向视图传递数据</h4><ol>
<li><p>路由必须通过控制器封装数据，在返回视图</p>
<pre><code class="hljs php">Route::get(<span class="hljs-string">&quot;url&quot;</span>,[Controller::class,<span class="hljs-string">&quot;demo&quot;</span>])</code></pre></li>
<li><p>传递数据三种方法</p>
<pre><code class="hljs php"><span class="hljs-keyword">return</span> view(<span class="hljs-string">&#x27;index&#x27;</span>,[key1=&gt;value1,key2=&gt;value2,...])	<span class="hljs-comment">//法1</span>
<span class="hljs-keyword">return</span> view(<span class="hljs-string">&#x27;index&#x27;</span>)-&gt;with([key1=&gt;value1,key2=&gt;value2,...])	<span class="hljs-comment">//法2</span>
<span class="hljs-keyword">return</span> view(<span class="hljs-string">&#x27;index&#x27;</span>)-&gt;with(key1,value1)-&gt;with(key2,value2)-&gt;...	<span class="hljs-comment">//法3</span></code></pre></li>
</ol>
<blockquote>
<p>以上三种方法太过冗余，php提供compact()：将多个变量打包成一个数组<br>就算一个变量是一个数组，也要通过compact进行打包</p>
</blockquote>
<pre><code class="hljs php"><span class="hljs-variable">$id</span>=<span class="hljs-number">1</span>;
<span class="hljs-variable">$name</span>=<span class="hljs-string">&quot;李四&quot;</span>;
<span class="hljs-variable">$date</span>=date(<span class="hljs-string">&quot;Y-m-d h:m:s&quot;</span>);
<span class="hljs-variable">$data</span>=compact(<span class="hljs-string">&#x27;id&#x27;</span>,<span class="hljs-string">&#x27;name&#x27;</span>,<span class="hljs-string">&#x27;date&#x27;</span>)
<span class="hljs-keyword">return</span> view(<span class="hljs-string">&#x27;index&#x27;</span>,<span class="hljs-variable">$data</span>)
<span class="hljs-comment">//	return view(&#x27;index&#x27;)-&gt;with($data)</span></code></pre>

<h4 id="插值语法的漏洞"><a href="#插值语法的漏洞" class="headerlink" title="插值语法的漏洞"></a>插值语法的漏洞</h4><blockquote>
<p>如果用户提交带html标签的数据,双花括号会显示其效果，解决方法如下：</p>
</blockquote>
<pre><code class="hljs php">&#123;!! data !!&#125;</code></pre>

<h4 id="模板继承"><a href="#模板继承" class="headerlink" title="模板继承"></a>模板继承</h4><blockquote>
<p>父视图</p>
</blockquote>
<pre><code class="hljs php">@<span class="hljs-keyword">yield</span>(<span class="hljs-string">&#x27;name&#x27;</span>)
@section(<span class="hljs-string">&#x27;name&#x27;</span>)
<span class="hljs-comment">// 可以书写html标签</span>
@show</code></pre>

<blockquote>
<p>子视图</p>
</blockquote>
<pre><code class="hljs php">@<span class="hljs-keyword">extends</span>(<span class="hljs-string">&quot;父视图&quot;</span>)
@<span class="hljs-keyword">yield</span>(<span class="hljs-string">&#x27;name&#x27;</span>,<span class="hljs-string">&#x27;value&#x27;</span>)
@section(<span class="hljs-string">&#x27;name&#x27;</span>)
<span class="hljs-comment">// 可以书写html标签</span>
@endsection</code></pre>

<h2 id="表单安全和用户认证"><a href="#表单安全和用户认证" class="headerlink" title="表单安全和用户认证"></a>表单安全和用户认证</h2><ol>
<li>防御CSRF攻击<blockquote>
<p>在表单中添加@csrf，前提:提交方式为post，路由为post</p>
</blockquote>
<pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">action</span>=<span class="hljs-string">&quot;&#123;&#123;route(&#x27;show2&#x27;)&#125;&#125;&quot;</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span>&gt;</span>
    姓名：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;name&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
    邮箱：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;email&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;email&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
    年龄：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;number&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;age&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
    爱好：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;hobby[]&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;足球&quot;</span>&gt;</span>足球
    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;hobby[]&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;篮球&quot;</span>&gt;</span>篮球
    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;hobby[]&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;排球&quot;</span>&gt;</span>排球<span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
    @csrf
    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;保存&quot;</span>&gt;</span>
    @if($errors-&gt;any())
        @foreach($errors-&gt;all() as $error)
            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>&#123;&#123;$error&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        @endforeach
    @endif
<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span></code></pre>
<blockquote>
<p>@csrf会生成一个令牌，在post请求下会进行令牌验证</p>
</blockquote>
</li>
</ol>
<p><img src="https://imgpp.com/s1/2022/04/04/imagee7fb4688db7bb8db.png" class="lazy" data-srcset="https://imgpp.com/s1/2022/04/04/imagee7fb4688db7bb8db.png" srcset=""></p>
<ol start="2">
<li><p>从csrf中排除例外路由</p>
<blockquote>
<p>设置一些路由不需要进行csrf验证<br>在/app/Http/Middleware/VerifyCsrfToken.php</p>
</blockquote>
<pre><code class="hljs php"><span class="hljs-keyword">protected</span> <span class="hljs-variable">$except</span> = [
	<span class="hljs-comment">// 排除的路由放到该数组</span>
	<span class="hljs-string">&#x27;/from2&#x27;</span>,
];</code></pre></li>
<li><p>自动验证</p>
<blockquote>
<p>创建表单验证请求类</p>
</blockquote>
<pre><code class="hljs shell">php artisan make:request 类名</code></pre>
<pre><code class="hljs php"><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">authorize</span>(<span class="hljs-params"></span>)</span>
<span class="hljs-function">//        判断当前用户是否有权限进行该请求</span>
<span class="hljs-function">    </span>&#123;
        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
    &#125;
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">rules</span>(<span class="hljs-params"></span>)</span>
<span class="hljs-function">//        定义规则</span>
<span class="hljs-function">    </span>&#123;
        <span class="hljs-keyword">return</span> [
                <span class="hljs-string">&#x27;name&#x27;</span>=&gt;[<span class="hljs-string">&#x27;required&#x27;</span>,<span class="hljs-string">&#x27;string&#x27;</span>,<span class="hljs-string">&#x27;min:2&#x27;</span>],
                <span class="hljs-string">&#x27;email&#x27;</span>=&gt;[<span class="hljs-string">&#x27;required&#x27;</span>,<span class="hljs-string">&#x27;email&#x27;</span>],
                <span class="hljs-string">&#x27;age&#x27;</span>=&gt;[<span class="hljs-string">&#x27;nullable&#x27;</span>,<span class="hljs-string">&#x27;integer&#x27;</span>,<span class="hljs-string">&quot;min:15&quot;</span>,<span class="hljs-string">&quot;max:120&quot;</span>],
                <span class="hljs-string">&#x27;hobby&#x27;</span>=&gt;[<span class="hljs-string">&#x27;required&#x27;</span>]
        ];
    &#125;
<span class="hljs-comment">//		定义错误信息    </span>
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">messages</span>(<span class="hljs-params"></span>)</span>
<span class="hljs-function">    </span>&#123;
        <span class="hljs-keyword">return</span> [
            <span class="hljs-string">&#x27;name.required&#x27;</span>=&gt;<span class="hljs-string">&#x27;自定义错误信息，姓名不能为空&#x27;</span>
        ];
    &#125;</code></pre></li>
</ol>
<blockquote>
<p>创建表单</p>
</blockquote>
<pre><code class="hljs php">&lt;form action=<span class="hljs-string">&quot;&#123;&#123;route(&#x27;show2&#x27;)&#125;&#125;&quot;</span> method=<span class="hljs-string">&quot;post&quot;</span>&gt;
    姓名：&lt;input type=<span class="hljs-string">&quot;text&quot;</span> name=<span class="hljs-string">&quot;name&quot;</span>&gt;&lt;br&gt;
    邮箱：&lt;input type=<span class="hljs-string">&quot;email&quot;</span> name=<span class="hljs-string">&quot;email&quot;</span>&gt;&lt;br&gt;
    年龄：&lt;input type=<span class="hljs-string">&quot;number&quot;</span> name=<span class="hljs-string">&quot;age&quot;</span>&gt;&lt;br&gt;
    爱好：&lt;input type=<span class="hljs-string">&quot;checkbox&quot;</span> name=<span class="hljs-string">&quot;hobby[]&quot;</span> value=<span class="hljs-string">&quot;足球&quot;</span>&gt;足球
    &lt;input type=<span class="hljs-string">&quot;checkbox&quot;</span> name=<span class="hljs-string">&quot;hobby[]&quot;</span> value=<span class="hljs-string">&quot;篮球&quot;</span>&gt;篮球
    &lt;input type=<span class="hljs-string">&quot;checkbox&quot;</span> name=<span class="hljs-string">&quot;hobby[]&quot;</span> value=<span class="hljs-string">&quot;排球&quot;</span>&gt;排球&lt;br&gt;
    @csrf
    &lt;input type=<span class="hljs-string">&quot;submit&quot;</span> value=<span class="hljs-string">&quot;保存&quot;</span>&gt;
    @<span class="hljs-keyword">if</span>(<span class="hljs-variable">$errors</span>-&gt;any())
        @<span class="hljs-keyword">foreach</span>(<span class="hljs-variable">$errors</span>-&gt;all() <span class="hljs-keyword">as</span> <span class="hljs-variable">$error</span>)
            &lt;li&gt;&#123;&#123;<span class="hljs-variable">$error</span>&#125;&#125;&lt;/li&gt;
        @<span class="hljs-keyword">endforeach</span>
    @<span class="hljs-keyword">endif</span>
&lt;/form&gt;</code></pre>
<blockquote>
<p>创建路由</p>
</blockquote>
<pre><code class="hljs php">Route::view(<span class="hljs-string">&quot;/from2&quot;</span>,<span class="hljs-string">&quot;verify&quot;</span>)-&gt;name(<span class="hljs-string">&quot;from2&quot;</span>);
Route::post(<span class="hljs-string">&quot;/show2&quot;</span>,[\App\Http\Controllers\ViewController::class,<span class="hljs-string">&quot;show2&quot;</span>])-&gt;name(<span class="hljs-string">&quot;show2&quot;</span>);</code></pre>
<blockquote>
<p>创建控制器</p>
</blockquote>
<pre><code class="hljs php"><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">show2</span>(<span class="hljs-params">VerifyRequest <span class="hljs-variable">$verifyRequest</span></span>)</span>&#123;
<span class="hljs-comment">//        $rules=[</span>
<span class="hljs-comment">//            &#x27;name&#x27;=&gt;[&#x27;required&#x27;,&#x27;string&#x27;,&#x27;min:2&#x27;],</span>
<span class="hljs-comment">//            &#x27;email&#x27;=&gt;[&#x27;required&#x27;,&#x27;email&#x27;],</span>
<span class="hljs-comment">//            &#x27;age&#x27;=&gt;[&#x27;nullable&#x27;,&#x27;integer&#x27;,&quot;min:15&quot;,&quot;max:120&quot;],</span>
<span class="hljs-comment">//            &#x27;hobby&#x27;=&gt;[&#x27;required&#x27;]</span>
<span class="hljs-comment">//        ];</span>
<span class="hljs-comment">////        $error=[</span>
<span class="hljs-comment">////            &#x27;name.required&#x27;=&gt;&#x27;姓名不能为空&#x27;</span>
<span class="hljs-comment">////        ];</span>
<span class="hljs-comment">//        $request-&gt;validate($rules);</span>
        dump(<span class="hljs-variable">$verifyRequest</span>-&gt;all());
&#125;</code></pre>




    </div>
     
    <div class="post-footer__meta"><p>更新于 2022-04-04</p></div> 
    <div class="post-entry__tags"><a href="/tags/%E8%B7%AF%E7%94%B1/" class="post-tags__link button"># 路由</a><a href="/tags/%E6%8E%A7%E5%88%B6%E5%99%A8/" class="post-tags__link button"># 控制器</a></div> 
</article>


    <div class="nav">
        <div class="nav__prev">
            
                <a href="/2022/01/27/PhpHotel%E9%A1%B9%E7%9B%AE/" class="nav__link">
                    <div>
                        <svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="24" height="24"><path d="M589.088 790.624L310.464 512l278.624-278.624 45.248 45.248L400.96 512l233.376 233.376z" fill="#808080"></path></svg>
                    </div>
                    <div>
                        <div class="nav__label">
                            <!-- Previous Post -->
                            上一页
                        </div>
                        <div class="nav__title">
                            PhpHotel项目
                        </div>
                    </div>
                </a>
            
        </div>
        <div class="nav__next">
            
        </div>
    </div>



    <div class="post__comments content-card" id="comment">
        
    <h4>评论</h4>
    
    
    
    <div id="valine_container" class="valine_thread"></div>

    
    
    
    
    
    
    
    


    </div>



</main>

            <footer class="footer" style="font-size: 10px;">
     
    <a href="#" class="button" id="b2t" aria-label="回到顶部" title="回到顶部">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" width="32" height="32">
            <path d="M233.376 722.752L278.624 768 512 534.624 745.376 768l45.248-45.248L512 444.128zM192 352h640V288H192z" fill="currentColor"></path>
        </svg>
    </a>

    


    
    
    
        <span id="busuanzi_container_site_uv" hidden>
            <span>游客:</span>
            <span id="busuanzi_value_site_uv"></span>
            <span></span>
            
                <span> </span>
            
        </span>
    
    
        <span id="busuanzi_container_site_pv" hidden>
            <span>次数:</span>
            <span id="busuanzi_value_site_pv"></span>
            <span></span>
            
        </span>
    
 
 

 
    
    
    <p>
        <!-- Powered by <a href="https://hexo.io" target="_blank">Hexo</a> | Theme - <a href="https://github.com/ChrAlpha/hexo-theme-cards" target="_blank">Cards</a> -->
        <span style="color: black;">作者：张程</span>
    </p>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
        var a_idx = 0;
            jQuery(document).ready(function($) {
                $("body").click(function(e) {
                    var a = new Array("会当凌绝顶，一览众山小", "春蚕到死丝方尽，蜡炬成灰泪始干", "夕阳无限好，只是近黄昏",
                     "位卑未敢忘忧国", "纸上得来终觉浅，绝知此事要躬行", "人生自古谁无死，留取丹心照汗青", "山重水复疑无路，柳暗花明又一村" ,
                     "小荷才露尖尖角，早有蜻蜓立上头", "床前明月光，疑是地上霜", "春色满园关不住，一枝红杏出墙来", 
                     "山际见来烟，竹中窥落日", "道狭草木长，夕露沾我衣","天长地久有时尽，此恨绵绵无绝期");
                    var color = new Array("#7B7B7B", "#CE0000", "#FF0080","#E800E8","#921AFF","#2828FF",
                    "#0072E3","#00CACA","#02DF82","#00DB00","#8CEA00","#E1E100","#EAC100","#F75000"
                    ,"#B87070","#AFAF61","#6FB7B7","#9999CC","#B766AD");
                    var $i = $("<a href='#' style='text-decoration:none'/>").text(a[a_idx]);
                    a_idx = (a_idx + 1) % a.length;
                    var x = e.pageX,
                        y = e.pageY;
                    $i.css({
                        "z-index": 999999999999999999999999999999999999999999999999999999999999999999999,
                        "top": y - 20,
                        "left": x,
                        "position": "absolute",
                        "font-weight": "bold",
                        "color": color[Math.floor(Math.random()*color.length)]
                    });
                    $("body").append($i);
                    $i.animate({
                            "top": y - 180,
                            "opacity": 0
                        },
                        1500,
                        function() {
                            $i.remove();
                        });
                });
            });
        </script>
    <script>
        !function() {
            function o(w, v, i) {
                return w.getAttribute(v) || i
            }
            function j(i) {
                return document.getElementsByTagName(i)
            }
            function l() {
                var i = j("script"),
                w = i.length,
                v = i[w - 1];
                return {
                    l: w,
                    z: o(v, "zIndex", -1),
                    o: o(v, "opacity", 0.5),
                    c: o(v, "color", "0,0,0"),
                    n: o(v, "count", 99)
                }
            }
            function k() {
                r = u.width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
                n = u.height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
            }
            function b() {
                e.clearRect(0, 0, r, n);
                var w = [f].concat(t);
                var x, v, A, B, z, y;
                t.forEach(function(i) {
                    i.x += i.xa,
                    i.y += i.ya,
                    i.xa *= i.x > r || i.x < 0 ? -1 : 1,
                    i.ya *= i.y > n || i.y < 0 ? -1 : 1,
                    e.fillRect(i.x - 0.5, i.y - 0.5, 1, 1);
                    for (v = 0; v < w.length; v++) {
                        x = w[v];
                        if (i !== x && null !== x.x && null !== x.y) {
                            B = i.x - x.x,
                            z = i.y - x.y,
                            y = B * B + z * z;
                            y < x.max && (x === f && y >= x.max / 2 && (i.x -= 0.03 * B, i.y -= 0.03 * z), A = (x.max - y) / x.max, e.beginPath(), e.lineWidth = A / 2, e.strokeStyle = "rgba(" + s.c + "," + (A + 0.2) + ")", e.moveTo(i.x, i.y), e.lineTo(x.x, x.y), e.stroke())
                        }
                    }
                    w.splice(w.indexOf(i), 1)
                }),
                m(b)
            }
            var u = document.createElement("canvas"),
            s = l(),
            c = "c_n" + s.l,
            e = u.getContext("2d"),
            r,
            n,
            m = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
            function(i) {
                window.setTimeout(i, 1000 / 45)
            },
            a = Math.random,
            f = {
                x: null,
                y: null,
                max: 20000
            };
            u.id = c;
            u.style.cssText = "position:fixed;top:0;left:0;z-index:" + s.z + ";opacity:" + s.o;
            j("body")[0].appendChild(u);
            k(),
            window.onresize = k;
            window.onmousemove = function(i) {
                i = i || window.event,
                f.x = i.clientX,
                f.y = i.clientY
            },
            window.onmouseout = function() {
                f.x = null,
                f.y = null
            };
            for (var t = [], p = 0; s.n > p; p++) {
                var h = a() * r,
                g = a() * n,
                q = 2 * a() - 1,
                d = 2 * a() - 1;
                t.push({
                    x: h,
                    y: g,
                    xa: q,
                    ya: d,
                    max: 6000
                })
            }
            setTimeout(function() {
                b()
            }, 100)
    } ();
    </script>
</footer>

        </div>
        
    <script defer src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.1.0/dist/lazyload.min.js"></script>
    <script>
        window.lazyLoadOptions = {
            elements_selector: ".lazy",
            threshold: 0
        };
    </script>
 

 

 

 

 



 



 


    
 


    
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script>

    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.4.1/dist/jquery.fancybox.min.css">

    
<script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.4.1/dist/jquery.fancybox.min.js"></script>

    <script>
        let lazyloadT = Boolean('true'),
            auto_fancybox = Boolean('false')
        if (auto_fancybox) {
            $(".post__content").find('img').each(function () {
                var element = document.createElement("a");
                $(element).attr("data-fancybox", "gallery");
                $(element).attr("href", $(this).attr("src"));
                if (lazyloadT) {
                    $(element).attr("href", $(this).attr("data-srcset"));
                }
                $(this).wrap(element);
            });
        } else {
            $(".post__content").find("fancybox").find('img').each(function () {
                var element = document.createElement("a");
                $(element).attr("data-fancybox", "gallery");
                $(element).attr("href", $(this).attr("src"));
                if (lazyloadT) {
                    $(element).attr("href", $(this).attr("data-srcset"));
                }
                $(this).wrap(element);
            });
        }
    </script>
 

 

 

 

 


    

    
    
    
    <script>
        function loadComment() {
            let e;
            (e = document.createElement("script")).src = 'https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js',
            document.body.appendChild(e);
            e.onload = () => {
                var valineConfig = {"appId":"c21JqSBfScFdueO44Scwp9Ba-gzGzoHsz","appKey":"apETP0QIekHzRXjTHvOnrQmS","placeholder":null,"path":null,"avatar":"retro","meta":["nick","mail","link"],"pageSize":null,"lang":null,"visitor":null,"highlight":null,"avatarForce":null,"recordIP":null,"serverURLs":null,"enableQQ":true,"requiredFields":["nick","mail"],"emojiCDN":null,"emojiMaps":null};
                valineConfig.el = '#valine_container';
                for (var i in valineConfig) {
                    if (valineConfig[i] === null) delete valineConfig[i];
                }
                new Valine(valineConfig);
            };
        }
    
        var runningOnBrowser = typeof window !== "undefined";
        var isBot = runningOnBrowser && !("onscroll" in window) || typeof navigator !== "undefined" && /(gle|ing|ro|msn)bot|crawl|spider|yand|duckgo/i.test(navigator.userAgent);
        var supportsIntersectionObserver = runningOnBrowser && "IntersectionObserver" in window;
    
        setTimeout(function () {
            if (!isBot && supportsIntersectionObserver) {
                var comment_observer = new IntersectionObserver(function(entries) {
                    if (entries[0].isIntersecting) {
                        loadComment();
                        comment_observer.disconnect();
                    }
                }, { threshold: [0] });
                comment_observer.observe(document.getElementById('comment'));
            } else {
                loadComment();
            }
        }, 1);
    </script>


    
    
    
    
    

    
    
    
    
    

    
    
    



    </body>
</html>
